package org.syh.boyangfighthealthclub.controller;

import org.syh.boyangfighthealthclub.entity.User;
import org.syh.boyangfighthealthclub.service.IUserService;
import org.syh.boyangfighthealthclub.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @Author: Ben
 * @Date: 2021/3/11 11:01
 */
@WebServlet("/userRegister")
public class UserRegister extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        res.setContentType("text/html;utf-8");
        String nickname = req.getParameter("nickName");
        String userName = req.getParameter("userName");
        String password = req.getParameter("password");
        String email = req.getParameter("email");
        String sex = req.getParameter("sex");
        String authCode = req.getParameter("authcode");
        HttpSession session = req.getSession();
        String randStrRegister = (String) session.getAttribute("randStrRegister");
        System.out.println(randStrRegister);
        System.out.println(authCode);
        if (authCode.equals(randStrRegister)) {
            Integer age = null;
            boolean test;
            try {
                age = Integer.valueOf(req.getParameter("age"));
                test = true;
            } catch (Exception e) {
                e.printStackTrace();
                test = false;
            }
            if ("".equals(nickname) || "".equals(userName) || "".equals(password) || "".equals(email) || "".equals(sex) || !test) {
                res.sendRedirect("UserIndex.jsp");
            }
            IUserService iUserService = new UserServiceImpl();
            boolean judgment = iUserService.insertUser(new User(nickname, userName, password, email, sex, age, "用户"));
            if (judgment) {
                System.out.println("注册成功");
                res.sendRedirect("UserIndex.jsp");
            }
        }
        req.setAttribute("msg","用户注册失败");
        res.sendRedirect("UserIndex.jsp");
    }
}
